package cn.iocoder.yudao.module.team.controller.admin.idcard;

import cn.iocoder.yudao.module.team.dal.dataobject.orde.OrdeDO;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.team.controller.admin.idcard.vo.*;
import cn.iocoder.yudao.module.team.dal.dataobject.idcard.IdcardDO;
import cn.iocoder.yudao.module.team.service.idcard.IdcardService;

@Tag(name = "管理后台 - 客人身份证信息")
@RestController
@RequestMapping("/team/idcard")
@Validated
public class IdcardController {

    @Resource
    private IdcardService idcardService;

    @PostMapping("/create")
    @Operation(summary = "创建客人身份证信息")
    @PreAuthorize("@ss.hasPermission('team:idcard:create')")
    public CommonResult<Integer> createIdcard(@Valid @RequestBody IdcardSaveReqVO createReqVO) {
        return success(idcardService.createIdcard(createReqVO));
    }

    @PutMapping("/createBatch")
    @Operation(summary = "更新客人身份证信息")
    @PreAuthorize("@ss.hasPermission('team:idcard:update')")
    public CommonResult<Boolean> createBatch(@Valid @RequestBody List<IdcardDO> updateReqVO) {
        idcardService.createBatch(updateReqVO);
        return success(true);
    }
    @PutMapping("/update")
    @Operation(summary = "更新客人身份证信息")
    @PreAuthorize("@ss.hasPermission('team:idcard:update')")
    public CommonResult<Boolean> updateIdcard(@Valid @RequestBody IdcardSaveReqVO updateReqVO) {
        idcardService.updateIdcard(updateReqVO);
        return success(true);
    }
    @DeleteMapping("/delete")
    @Operation(summary = "删除客人身份证信息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('team:idcard:delete')")
    public CommonResult<Boolean> deleteIdcard(@RequestParam("id") Integer id) {
        idcardService.deleteIdcard(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除客人身份证信息")
                @PreAuthorize("@ss.hasPermission('team:idcard:delete')")
    public CommonResult<Boolean> deleteIdcardList(@RequestParam("ids") List<Integer> ids) {
        idcardService.deleteIdcardListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得客人身份证信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('team:idcard:query')")
    public CommonResult<IdcardRespVO> getIdcard(@RequestParam("id") Integer id) {
        IdcardDO idcard = idcardService.getIdcard(id);
        return success(BeanUtils.toBean(idcard, IdcardRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得客人身份证信息分页")
    @PreAuthorize("@ss.hasPermission('team:idcard:query')")
    public CommonResult<PageResult<IdcardRespVO>> getIdcardPage(@Valid IdcardPageReqVO pageReqVO) {
        PageResult<IdcardDO> pageResult = idcardService.getIdcardPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, IdcardRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出客人身份证信息 Excel")
    @PreAuthorize("@ss.hasPermission('team:idcard:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportIdcardExcel(@Valid IdcardPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<IdcardDO> list = idcardService.getIdcardPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "客人身份证信息.xls", "数据", IdcardRespVO.class,
                        BeanUtils.toBean(list, IdcardRespVO.class));
    }

}